#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include "P2PTunnelAPIs.h"

typedef struct st_AuthData {
    char szUsername[64];
    char szPassword[64];
} sAuthData;

#define TUNNEL_USERNAME "admin"
#define TUNNEL_PASSWORD "testpwd"

void TunnelClientAuthentication(char *cszAccount, uint32_t nAccountMaxLength, char *cszPassword, uint32_t nPasswordMaxLength, const void *pArg) {
//    sTunnelInfo *info = (sTunnelInfo *)pArg;
    strcpy(cszAccount, TUNNEL_USERNAME);
    strcpy(cszPassword, TUNNEL_PASSWORD);
}

int P2PTunnelAgent_Connect_Wrap(const char *cszUID){
    int ret = P2PTunnelAgent_Connect_Ex(cszUID, TunnelClientAuthentication, 0);
    if (ret == TUNNEL_ER_REMOTE_NOT_SUPPORT_DTLS) {
        sAuthData authData = {.szUsername = {0}, .szPassword = {0}};
        int nErrFromDeviceCB = 0;
        strcpy(authData.szUsername, "Tutk.com");
        strcpy(authData.szPassword, "P2P Platform");
        int ret2 = P2PTunnelAgent_Connect(cszUID, (void *)&authData, sizeof(sAuthData), &nErrFromDeviceCB);
        return ret2;
    }
    return ret;

}
//int P2PAgentInitialize_Wrap(uint32_t max){
////    FILE *fp = fopen("/234.tx","r"); //读一个不存在的文件
////    if (fp){
////        return 0;
////    }else{
////        return -1;
////    }
////    return 0;
//
//    P2PTunnelAgent_Set_Log_Path("/tmp/1.log",12000);
//
//    int ret = P2PTunnelAgentInitialize(max);
//    return ret;
//}